<?php

class CF_AddBox{

	var $fieldspec;
	var $table_name;
	var $actionpage;
	var $thefilters;
	var $filterprefix;
	var $actval_prefix;
	var $top_dir;
	
	
	
	
	function CF_AddBox($fieldspec, $tablename, &$thefilters, $actionpage){
		$this->fieldspec = $fieldspec;
		$this->table_name = $table_name;
		$this->thefilters = &$thefilters;
		$this->actionpage = $actionpage;
	
	}



// NOTE:  Add procedure adds whatever it is passed.  It is up to the add form to send the correct items.  If something is filterd, it needs to be sent as hidden.

	/**
	*
	* make_add_box Method to Add box with add button and 0 or more editable
	* variable input boxes.  Depending on variables in fieldset, this should
	* support both new records from new primary key, or new multi-column
	* Any record with fieldspec[col]['Add'] will display an edit box also.
	* key records (select keys from other tables for a link table).
	* @param string $addtype  {'new', 'entries'}new = make up new key values, entries = show edit box for keys
	* @param bool $set_to_filter If true (default), filtered values will be passed as seting values.  If False, they wont.
	*
	*/
    function make_add_box($addtype = 'new'){
		// set non_entry values for default of no-value  $addtype is always new, take out sometime
		// CT 2009-10-22 - If you have two keys, just be sure to always specify add.  taking out logic to do it for you
		$none_entry[1]['keyvalue'] = '';
		$none_entry[1]['showvalue'] = '';

        $count = 0;
        $addform .= "<form name='$this->table_name" . "_Add' action='$this->actionpage' method='GET'>\n";
		$addform .= $this->addmarkup;
        foreach($this->fieldspec AS $col_name => $colspec){
		// First, check if filter is set.  If it is, make a hidden add column
			if((!empty($this->filters[$this->filterprefix . $col_name])) OR $this->filters[$this->filterprefix . $col_name] === 0){
				$addform .= "<input type='hidden' name='" . $col_name . "' value='" . $this->filters[$this->filterprefix . $col_name] [val] . "' />\n";
				// may add an overrid for (add form no matter what), but easiest to hard-code form in this case
				}
			else{ 
				// If filter is not set, add a box if 'add' is specified
				if(!empty($colspec['add'])){
				   (empty($colspec['headname']))?$elements[$count] ['head']= $col_name:$elements[$count] ['head']= $colspec['headname'];
				   $elements[$count] ['body'] = MakeEditField($colspec,$this->actval_prefix . $col_name,'', '', $this->actval_prefix . $col_name . '_ADD', $none_entry,$this->top_dir);
				   $count ++; 				
				}
			}
		}

        $elements[$count]['body'] = "<input type='submit' name='mode' value='Add' />";
		$addform .= $this->make_element_set($elements,"class='add_head'","class='add_body'");
		$addform .= "</form>";
	return $addform;

   }


    function OLDmake_add_box($addtype = 'new', $set_to_filter=true){
		// set non_entry values for default of no-value  $addtype is not used
		$none_entry[1]['keyvalue'] = '';
		$none_entry[1]['showvalue'] = '';

        $count = 0;

        $addform .= "<form name='$this->table_name" . "_Add' action='$this->actionpage' method='GET'>";
        foreach($this->fieldspec AS $col_name => $colspec){
            if((!empty($colspec['add']) OR (($addtype == 'entries') AND (!empty($colspec['key'] ))))){
               (empty($colspec['headname']))?$elements[$count] ['head']= $col_name:$elements[$count] ['head']= $colspec['headname'];
               $elements[$count] ['body'] = MakeEditField($colspec,$this->actval_prefix . $col_name,'', '', $this->actval_prefix . $col_name . '_ADD', $none_entry,$this->top_dir);
               $count ++; // below maybe replace with isset 
            } else if((!empty($this->filters[$this->filterprefix . $col_name])) OR $this->filters[$this->filterprefix . $col_name] == 0){
				$addform .= "<input type='hidden' name='" . $col_name . "' value='" . $this->filters[$this->filterprefix . $col_name] [val] . "' />\n";
            } // could replace this with make_add_filter() if if that function proves usefull
		}
        $elements[$count]['body'] = "<input type='submit' name='mode' value='Add' />";
		$addform .= $this->make_element_set($elements,"class='add_head'","class='add_body'");
		$addform .= "</form>";
	return $addform;

   }   
   
   
   /*
	* make_add_filters()  This function will create a hidden input field for use in the add form which sets the variable if it's filter is set
	*
	*/
   function make_add_filter(){
	if((!empty($this->filters[$this->filterprefix . $col_name])) OR $this->filters[$this->filterprefix . $col_name] == 0){
				$addform .= "<input type='hidden' name='" . $col_name . "' value='" . $this->filters[$this->filterprefix . $col_name] [val] . "' />\n";
            }
			return $addform;
   }
	
	/**
	 *(Also in CF_Filter)
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }

}
?>